// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	_before: function() { // 通用预处理器

	},
	// 用户信息相关 ny-user
	//微信登录openid 验证openid是否存在
	async login(item) {
		const db = uniCloud.database();
		const collection = db.collection("ny-user");
		let appid = 'wx912903619c27f196'
		let secret = 'd21ee07337d9a0b75fd64a270a76657a'
		let url =
			`https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${item}&grant_type=authorization_code`

		let res = await uniCloud.httpclient.request(url, {
			dataType: "json"
		})
		let openid = res.data.openid
		let login = false
		let rea = await collection.where({
			'openid': openid
		}).get()
		if (rea.data.length != 0) {
			login = true
		}
		return {
			openid,
			login,
			rea
		}

	},
	// 添加用户
	async addwxUser(item) {
		const db = uniCloud.database();
		const collection = db.collection("ny-user");
		const res = await collection.add(item)
		return {
			code: 200,
			msg: '添加成功!!',
			data: res
		}
	},
	// 修改用户
	async updatewxUser(itam, item) {
		const db = uniCloud.database();
		const collection = db.collection("ny-user");
		let res = await collection.doc(itam).update(item)
		return {
			code: 200,
			msg: '修改成功!!',
			data: res
		}
	},
}